﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;

namespace TimerJobServerManager.Common
{
    /// <summary>
    /// 保存系统的配置文件
    /// </summary>
    public class ServerManagerConfig: ObservableObject
    {
        private string _SelectIPAddress;

        public string SelectIPAddress
        {
            get { return _SelectIPAddress; }
            set
            {
                Set("SelectIPAddress", ref _SelectIPAddress, value);
            }
        }

        private ObservableCollection<string> _IPAddressList;
        /// <summary>
        /// 列表信息
        /// </summary>
        public ObservableCollection<string> IPAddressList
        {
            get { return _IPAddressList; }
            set
            {
                Set("IPAddressList", ref _IPAddressList, value);
            }
        }

        private string _VersionUpdateURL;

        /// <summary>
        /// 版本更更新的url地址
        /// </summary>
        public string VersionUpdateURL
        {
            get { return _VersionUpdateURL; }
            set
            {
                Set("VersionUpdateURL", ref _VersionUpdateURL, value);
            }
        }

        private bool _ExitCloseServer;
        
        /// <summary>
        /// 退出时 关闭服务
        /// </summary>
        public bool ExitCloseServer
        {
            get { return _ExitCloseServer; }
            set
            {
                Set("ExitCloseServer", ref _ExitCloseServer, value);
            }
        }

        private bool _StartUpOpenServer;

        /// <summary>
        /// 启动时打开服务
        /// </summary>
        public bool StartUpOpenServer
        {
            get { return _StartUpOpenServer; }
            set
            {
                Set("StartUpOpenServer", ref _StartUpOpenServer, value);
            }
        }
        

        private static string path = AppDomain.CurrentDomain.BaseDirectory + "\\Config\\ServerManagerConfig.json";

        private static JSONConfig<ServerManagerConfig> _jSONConfig;

        public static ServerManagerConfig _config;

        private static void SetDefaultValue(ServerManagerConfig set)
        {
            set.IPAddressList = new ObservableCollection<string>() { "127.0.0.1:12345"};
            set.VersionUpdateURL = "http://data.imchenchao.com/winDate/DataMonitor/update.xml";
        }

        public static ServerManagerConfig Default
        {
            get
            {
                if (_jSONConfig == null)
                {
                    _jSONConfig = new JSONConfig<ServerManagerConfig>(path);
                    //如果文件不存在则设置默认值
                    if (_jSONConfig.IsFileExists == false)
                    {
                        SetDefaultValue(_jSONConfig.Default);
                        _jSONConfig.Save();
                    }
                    _config = _jSONConfig.Default;
                }
                return _config;
            }
        }

        public static void Save()
        {
            if (_jSONConfig != null)
            {
                _jSONConfig.Save();
            }
        }
    }
}
